const { Model, DataTypes } = require('sequelize')
const { createFields } = require('../helper')

module.exports = (sequelize) => {
  class Role extends Model {
    static associate({ User, Menu }) {
      Role.belongsToMany(User, {
        as: 'userList',
        through: 'sys_user_role',
        constraints: false,
      })
      Role.belongsToMany(Menu, {
        as: 'menuList',
        through: 'sys_role_menu',
        constraints: false,
      })
    }
  }
  Role.init(
    createFields({
      name: { type: DataTypes.STRING, comment: '角色名称' },
      code: { type: DataTypes.STRING, comment: '角色编码' },
      description: {
        type: DataTypes.STRING(255),
        comment: '角色描述',
        allowNull: true,
      },
    }),
    {
      sequelize,
      tableName: 'sys_role',
      timestamps: true,
      modelName: 'Role',
    }
  )
  return Role
}
